/**
 * 签到页面
 * author:yinht
 * date:2017年6月2日19:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");

	var addmore = {
		addedItem:[],
		unaddItem:[],
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$("#return_addmore").unbind().on("click", function() {
				addmore.unaddItem=[];
				window.history.back(-1);
			});
			$("#addItem_submit").unbind().on("click", function() {
				$(".check_addItem").each(function(){
					if($(this).prop("checked")) {
						var item ={
							value:$(this).attr("itemvalue"),
							key:$(this).attr("itemkey1")
						}
						addmore.addedItem.push(item);
					}
				});
				var str =JSON.stringify(addmore.addedItem);
				window.localStorage.setItem("addedItem",str);
				addmore.unaddItem=[];
				window.history.back(-1);
				
			});
			
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var from = "right"; //从右边过来
				if(page_name == "newmenber"){
					from = "left";
				}
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: from
				});
			});
			Path.listen();
		},

		/**
		 * 页面数据加载
		 */
		loadData: function() {
			
//			新增
			var pk_user = window.localStorage.getItem("pk_user");
			var param = {
				"pk_user": pk_user

			}
			common.loadData({
				url: "/service/RRTPubServlet?method=initialize", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: addmore.InitializeCallBack, //回调函数
				dataSource: "Initialize",
				cache: false
			});
		},
		InitializeCallBack: function(json) {
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				//拉取所有可添加条目
				var userstr = window.localStorage.getItem("userInfo");
				var added = window.localStorage.getItem("addedItem");
				var userObj = JSON.parse(userstr);
				var keyArray= Object.keys(userObj.data);
				for (var i=0;i<json.data.entry.length;i++) {
					var item  = json.data.entry[i];
					for(var j =0;j<keyArray.length;j++){
						var keyItem = keyArray[j];
						if(userObj.data[keyItem] ==""){
							//为空字符串时  为未添加
							
							
							if(item.key == keyItem){
								addmore.unaddItem.push(item);
								break;
							}
							
						}else{
							if(item.key == keyItem){
								addmore.addedItem.push(item);
								break;
							}
						}
					}
					
				}
				var html ="";
				for(var k=0;k<addmore.unaddItem.length;k++){
					var item = addmore.unaddItem[k];
					html +='<div class="add_items"><div class="items_cont">'
					+'<span class="lf">'+item.value+'</span>'
					+'<span class="rf"><input class="check_addItem" itemkey1="'+item.key+'"  itemvalue="'+item.value+'" type="checkbox" /></span></div></div>';
				}
				$(".items_key").html(html);
				$(".check_addItem").unbind().on("click", function(event) {
					event.stopPropagation();
				});
				$(".add_items").unbind().on("click", function() {
					if($(this).find("input").is(":checked")){
						$(this).find("input").removeAttr("checked");
					}else{
						$(this).find("input").prop("checked",'true');
					}
					
				});

				var iscroll = require("iscroll");
				common.initIscroll({
					container: "iscrollContent_addmore",
					isVertical: true,
					showScrollBar: true
				});
			} else {
				common.showTip({
					content: json.message
				});
			}
			
		},
		
		/**
		 * each page init mothed
		 */
		initialize: function() {

			this.loadData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = addmore;
});

